--region *.lua
--Date
--此文件由[BabeLua]插件自动生成

local BattleData = class("BattleData");

function BattleData:ctor()
    self.wBattleId = 0;
    self.mapId = 0;
    self.members = {};
    self.frame = 0;
    self.actions = {};
    self.doSpecialAction = false;
    self.frameCbk = handler(self,self.addFrame);
end

function BattleData:roomInfo(data)
    mgrs.player:reset();
    self.members = {};
    self.wBattleId = data.wBattleId;
    self.mapId = data.mapId;
    local members = data.members;
    for key, var in pairs(members) do
         if var.idkey == HttpData.idkey then
            mgrs.player:setMyChairId(var.pos);
         end
         table.insert(self.members,var);
    end
    mgrs.wnd:openWnd(enums.Wnd.Loading);
    mgrs.wnd:openWnd(enums.Wnd.Battle,{num = mgrs.player:getMaxPlayerNums(),kind = mgrs.player:getBattleChose()});
end

function BattleData:roomMembers(data)
    local members = data.members;
    for key, var in pairs(members) do
        table.insert(self.members,var);
    end
end

function BattleData:roleOperationResult(data)
    local errorCode = data.opResult;
    if errorCode < 0 then
        mgrs.hint:showHint(string.format("报错啦,错误码是 %d",errorCode));
        mgrs.behave:nextBehave(enums.Behave.Hint);  
    end
end

function BattleData:getMemberByPos(pos)
    local member = nil;
    for key, var in pairs(self.members) do
        if var.pos == pos then
            member = var;
        end
    end
    return member;
end

function BattleData:getMembers()
    return self.members;
end

function BattleData:battlePause()
    mgrs.scheduler:delFrameCbk(self.frameCbk);
end

function BattleData:battleContinue()
    mgrs.scheduler:addFrameCbk(self.frameCbk);
end

function BattleData:battleReset()
    
end

function BattleData:battleFrameSync(data)
    local actions = data.actions;
    --第一个action很特殊 
    if not self.doSpecialAction then
        self.doSpecialAction = true;
        local action = actions[1];
        if action.cmd == battle_command.BATTLE_COMMAND_PLAYER_ENTER then
            local eframe = action.eframe;
            local pos = action.pos;
            --房间里边人员信息
            self.frame = eframe;
            mgrs.scheduler:addFrameCbk(self.frameCbk);
        end
    end
    for key, var in pairs(actions) do
        table.insert(self.actions,var);
    end
end

function BattleData:doFrameActionEnd(doAction)
    for key, var in pairs(self.actions) do
        if var == doAction then
            table.remove(self.actions,key);
            break;
        end
    end
end


function BattleData:playerExitRoom(pos)
    for key, var in pairs(self.members) do
        if var.pos == pos then
            table.remove(self.members,key);
            break;
        end
    end
end


function BattleData:getCurFrame()
    return self.frame;
end

function BattleData:addFrame()
    
    self.frame = self.frame + 1;
end

function BattleData:getActions()
    return self.actions;
end

return BattleData;
--endregion
